package com.chuanye.jiguangimdemo.activitys;



import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.activeandroid.query.Update;
import com.chuanye.jiguangimdemo.R;
import com.chuanye.jiguangimdemo.database.FriendEntry;
import com.chuanye.jiguangimdemo.utils.pinyin.HanyuPinyin;

import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.callback.GetUserInfoCallback;
import cn.jpush.im.android.api.model.UserInfo;
import cn.jpush.im.api.BasicCallback;

/**
 * 设置备注页面
 */
public class SetNoteNameActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "SetNoteNameActivity";
    private Context mContext;
    private ImageView setnotename_back_iv;
    private Button setnotename_finish_btn;
    private EditText setnotename_notename_et;
    private String noteName;
    private String userName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_note_name);
        mContext = this;

        Intent intent = getIntent();
        noteName = intent.getStringExtra("note");
        userName = intent.getStringExtra("user");

        initView();
    }

    private void initView() {
        //返回
        setnotename_back_iv = findViewById(R.id.setnotename_back_iv);
        setnotename_back_iv.setOnClickListener(this);
        //完成 提交
        setnotename_finish_btn = findViewById(R.id.setnotename_finish_btn);
        setnotename_finish_btn.setOnClickListener(this);
        //备注输入框
        setnotename_notename_et = findViewById(R.id.setnotename_notename_et);

        //设置备注
        setnotename_notename_et.setText(noteName);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.setnotename_back_iv://返回
                finish();
                break;
            case R.id.setnotename_finish_btn://完成
                final String name = setnotename_notename_et.getText().toString();

                //获取用户信息
                JMessageClient.getUserInfo(userName, new GetUserInfoCallback() {
                    @Override
                    public void gotResult(int responseCode, String responseMessage, final UserInfo info) {
                        if (responseCode == 0) {
                            //更新用户备注
                            info.updateNoteName(name, new BasicCallback() {
                                @Override
                                public void gotResult(int responseCode, String responseMessage) {
                                    if (responseCode == 0) {
                                        Intent intent = new Intent();
                                        intent.putExtra("updateName", name);
                                        setResult(1, intent);
                                        Toast.makeText(mContext, "更新成功",Toast.LENGTH_SHORT).show();
                                        //更新备注名时候同时更新数据库中的名字和letter
                                        new Update(FriendEntry.class).set("DisplayName=?", name).where("Username=?", userName).execute();
                                        new Update(FriendEntry.class).set("NoteName=?", name).where("Username=?", userName).execute();
                                        String newNote = HanyuPinyin.getInstance().getStringPinYin(name.substring(0, 1));
                                        new Update(FriendEntry.class).set("Letter=?", newNote.toUpperCase()).where("Username=?", userName).execute();
                                        finish();
                                    } else {
                                        Toast.makeText(mContext, "更新失败" + responseMessage,Toast.LENGTH_LONG).show();
                                    }
                                }
                            });
                        }
                    }
                });
                break;
        }
    }
}
